package com.grf.repository.impl;

import com.grf.pojo.User;
import com.grf.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserRepositoryImpl implements UserRepository {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public User getUser(String userName) {
        Query query = Query.query(Criteria.where("userName").is(userName));
        return mongoTemplate.findOne(query, User.class);
    }

    @Override
    public List<User> getAllUser() {
        return mongoTemplate.findAll(User.class);
    }

    @Override
    public void insertUser(User user) {
        mongoTemplate.insert(user);
        //mongoTemplate.save(user);
    }

    @Override
    public void insertMultiUser(List<User> userList) {
        mongoTemplate.insert(userList, "userList");
    }

    @Override
    public void updateUser(User user) {
        Query query = Query.query(Criteria.where("userName").is(user.getUserName()));
        Update update = Update.update("userName","王五");
        mongoTemplate.updateFirst(query,update,User.class);
        //mongoTemplate.updateMulti(query,update,User.class);
    }

    @Override
    public void removeUser(String userName) {

    }

}
